from nonebot import on_startswith
from nonebot.adapters import Event
from nonebot.adapters.onebot.v11 import MessageSegment
from .compile import Compile
import re











command = on_startswith(("/latex -q", "/latex --quick"))

@command.handle()
async def handler(event: Event):
    message = event.get_message().extract_plain_text().replace("/latex -q", "", 1).replace("/latex --quick", "", 1)

    # match = re.search(r"latex\s+-q(\s+e=([a-z]+))?", message)
    # if match:
    #     engine = match.group(2)
    # else:
    #     engine = None
    
    # # 如果需要，可以进行替换操作
    # message = re.sub(r"latex\s+-q(\s+e=[a-z]+)?", "", message)

    with open("./data/template.tex", "r", encoding="utf-8") as f:
        content = f.read()
        content = content.replace("{{ content }}", message)

    result = Compile(content).run()

    # if engine:
    #     result = Compile(content, engine).run()
    # else:
    #     result = Compile(content).run()

    for item in result:
        await command.finish(MessageSegment.image(item))
   